package com.example.toolbar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.util.AbToastUtil;
import com.example.toolbar.fragment.CityWideFragment;
import com.example.toolbar.fragment.Fragment2;
import com.example.toolbar.fragment.Fragmentmore;
import com.example.toolbar.fragment.fragmentBaoliao;
import com.example.toolbar.fragment.fragmentNews;
import com.example.toolbar.global.MyApplication;
import com.example.toolbar.net.HttpRequestWrap;
import com.example.toolbar.net.OnResponseHandler;
import com.example.toolbar.net.RequestHandler;
import com.example.toolbar.net.RequestStatus;
import com.example.toolbar.widget.PagerSlidingTabStrip;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MainActivity extends ActionBarActivity {
	private HttpRequestWrap httpRequest;
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;
	private ShareActionProvider mShareActionProvider;
	private Toolbar mToolbar;
	private FragmentTabHost tabHost;
	private static final int TAB_COUNT = 4;
	private Typeface iconfont;
	int icons[] = new int[] { R.string.xinwen, R.string.baoliao,
			R.string.tongcheng, R.string.more };
	Fragment[] fragments = new Fragment[] { new fragmentNews(),
			new fragmentBaoliao(), new CityWideFragment(), new Fragmentmore() };
	private String[] titles = { "新闻", "爆料", "同城", "更多" };


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewUtils.inject(this);
		// setSid();
		initViews();
		createTab();
	}

	public void createTab() {
		tabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
		tabHost.setup(this, getSupportFragmentManager(), R.id.real_content);
		for (int i = 0; i < fragments.length; i++) {
			TabHost.TabSpec tabSpec = tabHost.newTabSpec(titles[i]);
			tabSpec.setIndicator(getIndicator(i));
			tabHost.addTab(tabSpec, fragments[i].getClass(), null);

			tabHost.getTabWidget().setDividerDrawable(null);
			tabHost.setOnTabChangedListener(new TabChangeListener());
		}
	}

	public View getIndicator(int index) {
		LayoutInflater inflater = this.getLayoutInflater();
		View myindicator = inflater.inflate(R.layout.bottom_item, null);
		TextView item = (TextView) myindicator.findViewById(R.id.item);
		TextView icon = (TextView) myindicator.findViewById(R.id.icon);
		icon.setTypeface(iconfont);
		icon.setText(icons[index]);
		item.setText(titles[index]);
		if (index == 0) {
			item.setTextColor(Color.WHITE);
			icon.setTextColor(Color.WHITE);
		} else {
			item.setTextColor(Color.BLACK);
			icon.setTextColor(Color.BLACK);
		}
		return myindicator;
	}

	private void initViews() {
		httpRequest = new HttpRequestWrap(MainActivity.this);
		iconfont = Typeface.createFromAsset(getAssets(),
				"iconfont/iconfont.ttf");
		mToolbar = (Toolbar) findViewById(R.id.toolbar);
		mToolbar.setTitle("株洲plus");
		setSupportActionBar(mToolbar);
		mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				switch (item.getItemId()) {
				case R.id.action_settings:
					Toast.makeText(MainActivity.this, "action_settings", 0)
							.show();
					break;
				case R.id.action_share:
					Toast.makeText(MainActivity.this, "action_share", 0).show();
					break;
				default:
					break;
				}
				return true;
			}
		});
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		/* findView */
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				mToolbar, R.string.drawer_open, R.string.drawer_close);
		mDrawerToggle.syncState();
		mDrawerLayout.setDrawerListener(mDrawerToggle);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		/* ShareActionProvider閰嶇疆 */
		mShareActionProvider = (ShareActionProvider) MenuItemCompat
				.getActionProvider(menu.findItem(R.id.action_share));
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/*");
		mShareActionProvider.setShareIntent(intent);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// switch (item.getItemId()) {
		// case R.id.action_settings:
		// Toast.makeText(MainActivity.this, "action_settings", 0).show();
		// break;
		// case R.id.action_share:
		// Toast.makeText(MainActivity.this, "action_share", 0).show();
		// break;
		// default:
		// break;
		// }
		return super.onOptionsItemSelected(item);
	}

	class TabChangeListener implements OnTabChangeListener {
		/**
		 * TabSpec tabSpec = tabHost.newTabSpec(texts[index]);
		 * 
		 * @param tag
		 *            Tag
		 */
		@Override
		public void onTabChanged(String tag) {
			Log.d("========================", tag);
			// 获得
			TabWidget widget = tabHost.getTabWidget();

			for (int i = 0; i < TAB_COUNT; i++) {
				TextView curText = (TextView) widget.getChildTabViewAt(i)
						.findViewById(R.id.item);
				TextView icon = (TextView) widget.getChildTabViewAt(i)
						.findViewById(R.id.icon);
				if (curText.getText().equals(tag)) {
					curText.setTextColor(Color.WHITE);
					icon.setTextColor(Color.WHITE);
				} else {
					curText.setTextColor(Color.BLACK);
					icon.setTextColor(Color.BLACK);
				}
			}
		}

	}

	public void setSid() {
		httpRequest.setMethod(HttpRequestWrap.POST);
		httpRequest.setCallBack(new RequestHandler(this,
				new OnResponseHandler() {
					@Override
					public void onResponse(String result, RequestStatus status) {
						// ��ȡHttpUtils
						HttpUtils httpUtils = httpRequest.getHttpUtils();
						// ��ȡHttpClient
						DefaultHttpClient httpClient = (DefaultHttpClient) httpUtils
								.getHttpClient();
						// ���Cookie
						CookieStore cs = httpClient.getCookieStore();
						// ���Cookie���������м�ֵ��
						List<Cookie> list = cs.getCookies();
						if (list != null) {
							for (int i = 0; i < list.size(); i++) {
								Cookie c = list.get(i);
								if ("JSESSIONID".equals(c.getName())) {
									String sid = c.getValue();
									SharedPreferences sp = PreferenceManager
											.getDefaultSharedPreferences(MainActivity.this);
									Editor editor = sp.edit();
									editor.putString("session", sid);
									editor.commit();
									AbToastUtil.showToast(MainActivity.this,
											"登录成功");
									break;
								}
							}
						}
					}
				}));

		// post����
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", "言胜");
		params.put("password", "904042749");
		httpRequest.send("http://tr.zzapi.gson.cn/api/user/login", params);
	}

	@OnClick({ R.id.user_log_tou, R.id.user_message, R.id.user_collect,
			R.id.user_skin, R.id.user_change_password, R.id.user_set })
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.user_log_tou:
		case R.id.user_message:
		case R.id.user_collect:
		case R.id.user_skin:
		case R.id.user_change_password:
			intent.setAction("user_log");
			startActivity(intent);
			break;
		case R.id.user_set:
			intent.setAction("setUp");
			startActivity(intent);
			break;
		}
	}
}
